<?php
/*
Primary Author: Alex Arey
Secondary: Derek Shue, Sean Margison
Updated: 12/10/2011
Module Description: The appications controller is responsible for managing 
views of student internship applications for all users.
This handles views of INDIVIDUAL applications, and not summary queries.
*/
class ApplicationsController extends Zend_Controller_Action
{
	public function initAction(){
	//check user permissions
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$userID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
	}
	
	/**
	* Handles the display of the application, with supplemental data
	* and permissions checks based on user's role.
	*/
	public function viewAction(){
		$auth = Zend_Auth::getInstance(); 
		$userID = $auth->getIdentity()->getUID();//get UID
		$appID = $this->getRequest()->getParam('application');
		$application = new Application_Model_InternshipApp();
		$application->init($appID);
		
		//conditional checks for user permissions
		$utype =  $auth->getIdentity()->getUserType();
		$this->view->usertype = $utype;
		$this->view->application = null;
		if($utype == 'student'){//must be application of user
			if($application->_stuID == $userID)
				$this->view->application = $application;
		}
		if($utype == 'organization'){//must have suggested position belonging to partner
			$posmodel = new Application_Model_InternshipQuery();
			$position = $posmodel->selectInternship_ID($this->getRequest()->getParam('position'));
			$this->view->application = $application;
			$this->view->position = $position;
		}
		if($auth->getIdentity()->getUserRank() >= 2){//administrator, can see any
			$this->view->application = $application;
			$this->view->usertype = "admin";
		}
	}
	
	/**
	* Action for the Administrator to delete suggested applications for internship positions
	* Action requires application and position data in get parameters.
	*/
	public function deleteAction(){
		$auth = Zend_Auth::getInstance(); 
		$urank =  $auth->getIdentity()->getUserRank();
		if($urank<2){//check permissions. do nothing if insufficient permissions
			$this->_helper->redirector->gotoUrl('/applications/view/application/'.$appID);
			return;
		}
		$appID = $this->getRequest()->getParam('application');
		$posID = $this->getRequest()->getParam('position');
		$appaccept = new Application_Model_DbTable_ApplicationAcceptance();
		$appaccept->deleteSuggestedPosition($appID, $posID);
		$this->_helper->redirector->gotoUrl('/applications/view/application/'.$appID);
	}
	
	/**
	* Action for the Administrator to add suggested applications for internship positions
	* Action requires application and position data in get parameters.
	*/	
	public function addAction(){
		$auth = Zend_Auth::getInstance(); 
		$urank =  $auth->getIdentity()->getUserRank();
		if($urank<2){//check permissions. do nothing if insufficient permissions
			$this->_helper->redirector->gotoUrl('/applications/view/application/'.$appID);
			return;
		}
		$appID = $this->getRequest()->getParam('application');
		$this->view->appid = $appID;
		$dbtAppAccept = new Application_Model_DbTable_ApplicationAcceptance();
		$form = new Application_Form_InternAppAdmin();
		$this->view->form = $form;
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
            	//what to do with formdata
				$accept = $form->getValue('ACCEPT');
					foreach($form->getValue('SUGGESTIONS') as $posid){
						$dbtAppAccept->addUniqueSuggestedPosition($appID,$posid,null);//only add if not present.
					}
			}
			$this->_helper->redirector->gotoUrl('/applications/view/application/'.$appID);
		}
	}
	
	/**
	* Action for partners to accept or deny a suggested application for an internship.
	* Action requires application and position data in get parameters.
	*/
	public function partnerdecideAction(){
		$appID = $this->getRequest()->getParam('application');
		$posID = $this->getRequest()->getParam('position');
		$dbtAppAccept = new Application_Model_DbTable_ApplicationAcceptance();	
		$form = new Application_Form_AppDecide();
		$this->view->form = $form;
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
            	//what to do with formdata
				$accept = $form->getValue('ACCEPT');
				if($accept == 'y'){
					$dbtAppAccept->updateSuggestedPosition($appID,$posID,'c');
				}
				else if($accept == 'n')
					$dbtAppAccept->updateSuggestedPosition($appID,$posID,'d');
			}
			$this->_helper->redirector->gotoUrl('/applications/view/application/'.$appID.'/position/'.$posID);
		}
	}
	
	/**
	* Action for student to accept or turn down a position offered to them by a community partner.
	* Action requires application and position data in get parameters.
	*/
	public function studentdecideAction(){
		$appID = $this->getRequest()->getParam('application');
		$posID = $this->getRequest()->getParam('position');
		$dbtAppAccept = new Application_Model_DbTable_ApplicationAcceptance();	
		$form = new Application_Form_AppDecide();
		$this->view->form = $form;
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
            	//what to do with formdata
				$accept = $form->getValue('ACCEPT');
				if($accept == 'y'){
					$dbtAppAccept->updateSuggestedPosition($appID,$posID,'s');
				}
				else if($accept == 'n')
					$dbtAppAccept->updateSuggestedPosition($appID,$posID,'t');
			}
			$this->_helper->redirector->gotoUrl('/applications/view/application/'.$appID.'/position/'.$posID);
		}
	}
}
?>
